const weekdays = '日一二三四五六';
/**
 * 获取时间信息
 * @returns
 */
export const getDateTimeStr = () => {
  const date = new Date();

  const year = date.getFullYear();
  const month = date.getMonth() + 1;
  const monthStr = month >= 10 ? month : '0' + month;
  const day = date.getDate();
  const dayStr = day >= 10 ? day : '0' + day;
  const hours = date.getHours();
  const hourStr = hours >= 10 ? hours : '0' + hours;
  const minutes = date.getMinutes();
  const minutesStr = minutes >= 10 ? minutes : '0' + minutes;
  const seconds = date.getSeconds();
  const secondsStr = seconds >= 10 ? seconds : '0' + seconds;

  return `${year}-${monthStr}-${dayStr} ${hourStr}:${minutesStr}:${secondsStr}`;
};

export const getTimeInfo = () => {
  const date = new Date();

  const year = date.getFullYear();
  const month = date.getMonth() + 1;
  const monthStr = month >= 10 ? month : '0' + month;
  const day = date.getDate();
  const dayStr = day >= 10 ? day : '0' + day;
  const hours = date.getHours();
  const hourStr = hours >= 10 ? hours : '0' + hours;
  const minutes = date.getMinutes();
  const minutesStr = minutes >= 10 ? minutes : '0' + minutes;
  const seconds = date.getSeconds();
  const secondsStr = seconds >= 10 ? seconds : '0' + seconds;
  const weekdayNum = date.getDay();

  return {
    time: `${hourStr}:${minutesStr}:${secondsStr}`,
    date: `${year}年${monthStr}月${dayStr}日`,
    weekday: `星期${weekdays[weekdayNum]}`,
  };
};

export const getYearMonth = (date: Date = new Date()) => {
  const year = date.getFullYear();
  const month = date.getMonth() + 1;
  const monthStr = month >= 10 ? month : '0' + month;
  return `${year}-${monthStr}`;
};

/**
 * 字符转数字
 * @param str string
 * @returns
 */
export function str2Num(str: string | number) {
  return Number(str) || 0;
}

/**
 * 字符串数字转千分位数字
 * @param num string|number
 * @returns
 */
export function num2LocalStr(num: string | number) {
  return str2Num(num)
    .toString()
    .replace(/\d+/, function (n) {
      // 先提取整数部分
      return n.replace(/(\d)(?=(\d{3})+$)/g, function ($1) {
        return $1 + ',';
      });
    });
}
